package org.ccunix.eshop.controller.api;

import com.alibaba.fastjson.JSON;
import org.ccunix.eshop.domain.dto.CartVO;
import org.ccunix.eshop.domain.dto.MemberVO;
import org.ccunix.eshop.service.MemberServiceIface;
import org.ccunix.eshop.service.ShopCarServiceIface;
import org.ccunix.eshop.service.impl.MemberServiceImpl;
import org.ccunix.eshop.service.impl.ShopCarServiceImpl;
import org.ccunix.eshop.utils.AjaxResult;
import org.ccunix.eshop.utils.LogOutputUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/api/member")
public class MemberController extends HttpServlet {
    MemberServiceIface memberService = new MemberServiceImpl();
    ShopCarServiceIface shopCarService = new ShopCarServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method= request.getParameter("m");
        if ("login".equals(method)){
            login(request,response);
        }else if ("getLoginMember".equals(method)){
            getLoginMember(request,response);
        }else if ("exit".equals(method)){
            exit(request,response);
        }
    }

    private void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //清空session信息
        HttpSession session = request.getSession();
        if (session!=null){
            session.invalidate();//清空所有信息
            //session.removeAttribute("loginMember");//清空刚才的 指定的
        }
        AjaxResult result =AjaxResult.success("会员安全退出");
        response.getWriter().write(JSON.toJSONString(result));
    }

    //获得登录用户信息
    private void getLoginMember(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        HttpSession session = request.getSession();
        Object obj = session.getAttribute("loginMember");
        if (obj!=null){
            MemberVO memberVO = (MemberVO)obj;
            AjaxResult result = AjaxResult.success("登录成功",memberVO);
            response.getWriter().write(JSON.toJSONString(result));
        }else {
            AjaxResult result = AjaxResult.error(502,"session中不存在登录信息");
            response.getWriter().write(JSON.toJSONString(result));
        }
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String loginName = request.getParameter("loginName");
        String loginPwd = request.getParameter("loginPwd");
        //调用控制器 把这两个值传过去 返回一个对象 MemberVO
        MemberVO memberVO = memberService.login(loginName,loginPwd);
        AjaxResult result = null;
        if (memberVO!=null){
            //创建购物车信息 获得
            CartVO cartVO = shopCarService.getShopCart(memberVO.getId());
            LogOutputUtil.logger.info(loginName+"登录成功");
            result = AjaxResult.success("登录成功",memberVO);
            //把用户信息存到session中
            HttpSession session = request.getSession(true);//必须要创建session  null  创建session
            session.setAttribute("loginMember",memberVO);
            session.setAttribute("shopCart",cartVO);
        }else{
            LogOutputUtil.logger.error(loginName+"登录失败");
            result = AjaxResult.error(501,"用户名或者密码错误");
        }
        response.getWriter().write(JSON.toJSONString(result));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
